package com.xianyun.site.support.executor.handle.sms;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.xianyun.common.redis.service.RedisService;
import com.xianyun.site.support.common.constant.SmsServiceConstants;
import com.xianyun.site.support.domain.model.SmsMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;

/**
 * 短信策略
 * @author mac_zyj
 */
@Component
public abstract class BaseSmsStrategy {
    /**Redis服务*/
    @Autowired
    protected RedisService redisService;

    /**
     * 初始化短信发送请求
     * @param message 短信请求
     * @return 发送请求
     */
    public abstract SendSmsRequest initSendSmsRequest(SmsMessage message);

    protected void setSmsCache(String timesCheckKey){
        Integer redisGetTimes=redisService.getCacheObject(timesCheckKey);
        int sendTimesInLastHour= 0;
        if(null!=redisGetTimes){
            sendTimesInLastHour=redisGetTimes;
        }
        //设置当前短信发送次数间隔时间
        redisService.setCacheObject(timesCheckKey,++sendTimesInLastHour, SmsServiceConstants.CHECK_SMS_SEND_TIMES_TIME, TimeUnit.HOURS);
    }
}
